// pages/achievement/check/chech.js
import wxCharts from '../../../utils/wx-charts'
import { debounce } from '../../../utils/Tools'
import moment from '../../../utils/moment.js'
import { getSalesmanAchi, getClientAchi, getSalesmanTable } from '../../../network/other';
const app = getApp()
Page({
  data: {
    active: 0,
    date:[],
    way:[],
    screen:{
      date:0,way:0
    },
    search:'',
    isChart: false,
    userinfo: '',
    salesmanList:[],
    clientList:[],
    afterList:[],
    afterClient:[],
    offerInfo:{},
    categories:[],
    imageWidth:''
  },
  onLoad: function (options) {
    const imageWidth = wx.getSystemInfoSync().windowWidth / 375;
    this.handleFilter = debounce(this.handleFilter,300)
    this.setData({
      imageWidth,
      userinfo: wx.getStorageSync('user') || app.globalData.user
    })
    //      调用网络请求
    this._getCheckData()
  },
  //      事件函数     //
  forbidScroll() {
    return;
  },
  handleTabActive(e){   //  处理点击切换导航
    const { index } = e.detail
    this.setData({
      way:[],date:[],screen:{date: 0,way: 0}
    })
    switch (index) {
      case 0:
        this.setData({
          afterList: this.data.salesmanList
        })
        this.handleWay(this.data.salesmanList)
        this.handleDate(this.data.salesmanList)
        break;
      case 1:
        this.setData({
          afterClient: this.data.clientList
        })
        let way = [];
        if(this.data.clientList.length){
          this.data.clientList.forEach(res=>{
            res.product.forEach(result =>{
              way.push(result.pack_method)
            })
          })
          Array.from(new Set(way)).forEach((res,index)=>{
            this.setData({
              way:this.data.way.concat({text:res,value:index})
            })
          })
        }
        this.handleDate(this.data.clientList)     
        break;
    }
  },
  async handleSalesMenu(e){  //  处理下拉菜单选择
    const {type} = e.currentTarget.dataset
    this.setData({
      [`screen.${type}`]: e.detail
    })
    const data = {
      date: this.data.date[this.data.screen.date].text
    }
    const result = this.data.salesmanList.filter(res=>{
      return moment(res.create_time*1000,'YYYY.MM').includes(data.date)
    })
    if(!result) this.setData({afterList: this.data.salesmanList})
    this.setData({
      afterList: result
    })
  },
  async handleClientMenu(e){  //  处理下拉菜单选择
    const {type} = e.currentTarget.dataset
    this.setData({
      [`screen.${type}`]: e.detail
    })
    const data = {
      date: this.data.date[this.data.screen.date].text
    }
    const result = this.data.clientList.filter(res=>{
      return moment(res.create_time*1000,'YYYY.MM').includes(data.date)
    })
    if(!result) this.setData({afterClient: this.data.clientList})
    this.setData({
      afterClient: result
    })
  },
  handleChart(){
    this._getChart()
    this.setData({isChart: true})
  },
  onClose() {
    this.setData({ isChart: false });
  },
  handleFilter(e){
    const result = this.data.clientList.filter(res =>{
      return res.customer_name.includes(e.detail)
    })
    if(!result) this.setData({afterClient: this.data.clientList})
    this.setData({afterClient:result})
  },
  _getChart(){
    new wxCharts({
      canvasId: 'chart',
      type: 'column',
      dataLabel: false,
      categories: this.data.categories,
      series: this.data.offerInfo.series,
      width: (375 * this.data.imageWidth),
      height: 220
    });
  },
  handleWay(arr){
    let way = [];
    arr.forEach(res=>{
      way.push(res.pack_method)
    })
    Array.from(new Set(way)).forEach((res,index)=>{
      this.setData({
        way:this.data.way.concat({text:res,value:index})
      })
    })
  },
  handleDate(arr){
    let date = [];
    arr.forEach(res=>{
      const time = res.create_time*1000
      date.push(moment(time,'YYYY.MM'))
    })
    Array.from(new Set(date)).forEach((res,index)=>{
      this.setData({
        date:this.data.date.concat({text:res,value:index})
      })
    })
  },
  //      网络请求       //
  async _getCheckData(){
    const sale = await getSalesmanAchi()
    const client = await getClientAchi()
    const table = await getSalesmanTable()
    if(sale.code ===200 && client.code ===200){
      this.handleWay(sale.data)
      this.handleDate(sale.data)
      this.setData({
        salesmanList: sale.data,
        clientList: client.data,
        afterList: sale.data,
        offerInfo: table.data
      })
      let handleCate = [];
      this.data.offerInfo.categories.forEach(res=>{
        handleCate.push(moment(res*1000,'DD')+"日")        
      })
      this.setData({
        categories:handleCate
      })
    }
  }
})